#include "ustring.h"

#include <string.h>
#include <memory.h>
#include <stdlib.h>

typedef struct _STRING
{
	char *data;
	int length;
	UFO_String handle;
}STRING;

UFO_String ufoStringCreate()
{
	STRING *pString = malloc(sizeof(STRING));
	memset(pString, 0, sizeof(STRING));
	pString->handle = (UFO_String)ufoCreateHandle(UFO_ID_STRING, pString);

	return pString->handle;
}

int ufoStringAssign(UFO_String ustring, const char * text)
{
	STRING *pString = ufoObjectFromHandle((UFO_Handle)ustring);
	if (pString) {
		if (pString->data) {
			free(pString->data);
			pString->data = 0;
			pString->length = 0;
		}
		if (text) {
			pString->length = strlen(text);
			pString->data = malloc(pString->length+1);
			strcpy(pString->data, text);
		}
	}
	return 1;
}

int ufoStringDelete(UFO_String ustring)
{
	STRING *pString = ufoObjectFromHandle((UFO_Handle)ustring);
	if (pString) {
		if (pString->data) {
			free(pString->data);
			pString->data = 0;
			pString->length = 0;
		}
	}
	return 1;
}

const char* ufoStringGet( UFO_String ustring )
{
	STRING *pString = ufoObjectFromHandle((UFO_Handle)ustring);
	if (pString) {
		return pString->data;
	}
	return 0;
}
